package com.julis.db;

import android.content.Context;
import android.text.TextUtils;

import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.UpdateBuilder;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import com.julis.bean.ObuTrade_Info;
import com.julis.bean.Obu_Info;


/**
 * Created by luoliwen on 16/4/28.
 * 完成对数据库的CRUD的操作
 */
public class Z_ObuInfoDao {
    private Context context;
    private Dao<Obu_Info, Integer> obudao;
    private Z_DataBaseHelper helper;

    public Z_ObuInfoDao(Context context) {
        this.context = context;
        helper = Z_DataBaseHelper.getInstance(context);
        try {
            obudao = helper.getDao(Obu_Info.class);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    List<Obu_Info> account1 = new ArrayList<Obu_Info>();

    public List<Obu_Info> query(String checktimestart, String checktimeend ) {
        try {
            account1.clear();

                account1 = obudao.queryBuilder().where().between("device_check_time", checktimestart + " 00:00:01", checktimeend + " 23:59:59").query();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return account1;
    }

    public List<Obu_Info> queryvin(String vincode) {
        try {
            account1.clear();

            account1 = obudao.queryBuilder().where().eq("vincode", vincode).query();


        } catch (Exception e) {
            e.printStackTrace();
        }
        return account1;
    }
    public List<Obu_Info> querymac(String obu_code) {
        try {
            account1.clear();

            account1 = obudao.queryBuilder().where().eq("obu_code", obu_code).query();


        } catch (Exception e) {
            e.printStackTrace();
        }
        return account1;
    }

    public Obu_Info getObu_Info(String id) {
        try {
            Obu_Info account1 = obudao.queryBuilder().where().eq("id", id).query().get(0);
            return account1;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public List<Obu_Info> queryForAll() {
        try {
            List<Obu_Info> account1 = obudao.queryForAll();
            return account1;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 录入OBU
     *
     * @param vincode
     * @param obu_code
     * @param vin_input_time
     * @param obu_input_time
     * @param vin_input_success
     * @param obu_input_success
     * @param obu_check_success
     * @param ble_check_success
     * @param vin_write_success
     * @param device_check_time
     * @param check_people
     */
    public void addOBU(String vincode, String obu_code, String vin_input_time,
                       String obu_input_time, String vin_input_success,
                       String obu_input_success, String obu_check_success,
                       String ble_check_success, String vin_write_success,
                       String device_check_time, String check_people, String kl15_success) {

        try {
            UUID UUIDs = UUID.randomUUID();
            String uuid = UUIDs.toString().substring(0, 8);
            Obu_Info user = new Obu_Info(vincode, obu_code, vin_input_time,
                    obu_input_time, vin_input_success,
                    obu_input_success, obu_check_success,
                    ble_check_success, vin_write_success,
                    device_check_time, check_people, kl15_success);
            user.setId(uuid);
            user.setType("1");
            obudao.create(user);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
